import { defineStore } from "pinia";
import { shallowRef, reactive } from "vue";
import { ClassApi } from "@/api";

export default defineStore("class", () => {
    const list = reactive<Array<Vm.ClassModel>>([]);

    const add = async (model: Vm.ClassModel) => {
        const cls_id = await ClassApi.add(model)
        list.push({ ...model, cls_id })
    }
    const pagination = reactive({
        currentPage: 1,
        pageSize: 6,
        total: 0,
    })
    const search = reactive({
        cls_name: "",
        cls_dic_id_major: 0,
        cls_status: 0,
    });
    const updateList = (async (currentPage: number = 1) => {
        pagination.currentPage = currentPage;
        list.splice(0, list.length)
        const result = await ClassApi.getList({ ...search, begin: (pagination.currentPage - 1) * pagination.pageSize, pageSize: pagination.pageSize })
        list.push(...result.list);
        pagination.total = result.total;
        console.log()

    })
    const update = async (model: Vm.ClassModel) => {
        await ClassApi.update(model)
        const i = list.findIndex(item => item.cls_id === model.cls_id)
        list.splice(i, 1, { ...model })
    }

    return { list, add, update, pagination, updateList, search }
})